package unilearn.generate;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.Set;
import unilearn.generate.*;
import unilearn.generate.GenerateSubject;

/*
 * GenerateSQLData.java
 *
 * Created on 29 April 2008, 19:32
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

/**
 * Generates DATABASE INSERT Commands for testing
 * @author Ben Morrall, bemo56@hotmail.com
 * @version 1.0, 2008-04-29, blm
 */
public class GenerateSQLData
{

	public static void main(String[] args) throws IOException, SQLException
	{
		GenerateSQLData sqlGen = new GenerateSQLData();
		sqlGen.generate();
		sqlGen.close();
	}

	public static final int GENERATE_STUDENT_ID = 100;

	private GenerateFileData fileGen;
	private GenerateSubmission submissionGen;
	private GenerateAssignments assignmentGen;
	private GenerateSubject subjectGen;

	private PrintWriter writer;

	/** Creates a new instance of GenerateSQLData */
	public GenerateSQLData() throws IOException
	{
		File outputFile = new File("testOutput.sql");
		FileOutputStream fos = new FileOutputStream(outputFile);
		writer = new PrintWriter(new OutputStreamWriter(fos));
		System.out.println("Exporting SQL to " + outputFile.getAbsolutePath());

		fileGen = new GenerateFileData(writer);
		submissionGen = new GenerateSubmission(fileGen, writer);
		assignmentGen = new GenerateAssignments(fileGen, submissionGen, writer);
		subjectGen = new GenerateSubject(assignmentGen, writer);
	}

	public void close() throws IOException
	{
		writer.close();
		submissionGen.close();
	}

	public void generate() throws SQLException
	{
		Set<String> studentCodes = generateStudents();

		subjectGen.addSubjects(studentCodes);
	}

	private Set<String> generateStudents()
	{
		Set<String> studentCodes = new HashSet<String>();
		do
		{
			studentCodes.add(generateStudentID());
		} while (studentCodes.size() != GENERATE_STUDENT_ID);
		return studentCodes;
	}

	private String generateStudentID()
	{
		int val;

		StringBuilder builder = new StringBuilder();
		for (int i = 0; i < 6; i++)
		{
			val = (int) (Math.random() * 10.0);
			builder.append(val);
		}
		val = (int) (Math.random() * 11.0);
		if (val == 10)
		{
			builder.append("X");
		}
		else
		{
			builder.append(val);
		}

		return builder.toString();
	}

}
